package cn.meye11.crm.mapper;
import cn.meye11.crm.common.DateUtils;
import cn.meye11.crm.common.RegexUtils;
import cn.meye11.crm.common.StringUtils;
import cn.meye11.crm.domain.entity.ItemEntity;
import cn.meye11.crm.domain.query.ItemQuery;
import org.apache.ibatis.jdbc.SQL;

/**
 * 项目表(Item)表数据库访问层 SQL组合类
 * 
 * @author makejava
 * @since 2023-11-18 19:20:43
 */
 
public class ItemProvider {

    public String list(ItemQuery query) {

        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM(ItemEntity.Table + " ite ");
        sql.WHERE(wheres(query));
        sql.ORDER_BY(query.getSort() + " " + query.getDirection());
        return sql.toString();
    }

    public String count(ItemQuery query) {

        SQL sql = new SQL();
        sql.SELECT("count (id)");
        sql.FROM(ItemEntity.Table + " ite ");
        sql.WHERE(wheres(query));
        return sql.toString();
    }

    private String wheres(ItemQuery query) {
        StringBuilder where = new StringBuilder();
        if (query != null) {
            // 不为空时，必须为逗号分割的数字才能查询
            String id = StringUtils.commaDelimitedTrimEmptyString(query.getId());
            if (StringUtils.isNotBlank(id) && RegexUtils.check(RegexUtils.Type.ONLY_NUMBER_OR_EMPTY, id)) {
                where.append("ite.id in (" + id + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getName())) {
                where.append("ite.name = '" + query.getName() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getNameLike())) {
                where.append("ite.name like '%" + query.getNameLike() + "%'").append(" and ");
            }
            String nameIn = StringUtils.toSqlInString(query.getNameIn());
            if (StringUtils.isNotBlank(nameIn)) {
                where.append("ite.name in (" + nameIn + ")").append(" and ");
            }
            String nameNotIn = StringUtils.toSqlInString(query.getNameNotIn());
            if (StringUtils.isNotBlank(nameNotIn)) {
                where.append("ite.name not in (" + nameNotIn + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getCode())) {
                where.append("ite.code = '" + query.getCode() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getCodeLike())) {
                where.append("ite.code like '%" + query.getCodeLike() + "%'").append(" and ");
            }
            String codeIn = StringUtils.toSqlInString(query.getCodeIn());
            if (StringUtils.isNotBlank(codeIn)) {
                where.append("ite.code in (" + codeIn + ")").append(" and ");
            }
            String codeNotIn = StringUtils.toSqlInString(query.getCodeNotIn());
            if (StringUtils.isNotBlank(codeNotIn)) {
                where.append("ite.code not in (" + codeNotIn + ")").append(" and ");
            }
            if (query.getPrice() != null) {
                where.append("ite.price = " + query.getPrice()).append(" and ");
            }
            if (query.getUntaxedPrice() != null) {
                where.append("ite.untaxed_price = " + query.getUntaxedPrice()).append(" and ");
            }
            if (query.getTaxRate() != null) {
                where.append("ite.tax_rate = " + query.getTaxRate()).append(" and ");
            }
            if (StringUtils.isNotBlank(query.getServiceContent())) {
                where.append("ite.service_content = '" + query.getServiceContent() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getServiceContentLike())) {
                where.append("ite.service_content like '%" + query.getServiceContentLike() + "%'").append(" and ");
            }
            String serviceContentIn = StringUtils.toSqlInString(query.getServiceContentIn());
            if (StringUtils.isNotBlank(serviceContentIn)) {
                where.append("ite.service_content in (" + serviceContentIn + ")").append(" and ");
            }
            String serviceContentNotIn = StringUtils.toSqlInString(query.getServiceContentNotIn());
            if (StringUtils.isNotBlank(serviceContentNotIn)) {
                where.append("ite.service_content not in (" + serviceContentNotIn + ")").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabel())) {
                where.append("ite.label = '" + query.getLabel() + "'").append(" and ");
            }
            if (StringUtils.isNotBlank(query.getLabelLike())) {
                where.append("ite.label like '%" + query.getLabelLike() + "%'").append(" and ");
            }
            String labelIn = StringUtils.toSqlInString(query.getLabelIn());
            if (StringUtils.isNotBlank(labelIn)) {
                where.append("ite.label in (" + labelIn + ")").append(" and ");
            }
            String labelNotIn = StringUtils.toSqlInString(query.getLabelNotIn());
            if (StringUtils.isNotBlank(labelNotIn)) {
                where.append("ite.label not in (" + labelNotIn + ")").append(" and ");
            }
            if (query.getStart() != null) {
                where.append("ite.time >= '" + DateUtils.localDateTimeToStr(query.getStart()) + "'").append(" and ");
            }
            if (query.getEnd() != null) {
                where.append("ite.time <= '" + DateUtils.localDateTimeToStr(query.getEnd()) + "'").append(" and ");
            }
        }
        where.append("ite.del='0'");
        return where.toString();
    }
}
